package edu.hawaii.myisern.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.util.List;
import edu.hawaii.myisern.collaborations.jaxb.CollaboratingOrganizations;
import edu.hawaii.myisern.collaborations.jaxb.Collaboration;
import edu.hawaii.myisern.collaborations.jaxb.CollaborationTypes;
import edu.hawaii.myisern.collaborations.jaxb.OutcomeTypes;
import edu.hawaii.myisern.collaborations.jaxb.Years;
import edu.hawaii.myisern.organizations.jaxb.AffiliatedResearchers;
import edu.hawaii.myisern.organizations.jaxb.Organization;
import edu.hawaii.myisern.organizations.jaxb.ResearchKeywords;
import edu.hawaii.myisern.researchers.jaxb.Researcher;

/**
 * Demonstrates that the MyIsernModel is working.
 * 
 * @author Ed Kim
 * @author Jon Lao
 * @author Jianfei Liao
 * @author Jared Sunouchi
 */
public class TestMyIsernModel {
  /** The test object instance. */
  MyIsernModel myIsernModel = MyIsernModel.getInstance();
  /** A string for "user.dir". */
  private static final String USER_DIR = "user.dir";
  
  /**
   * Initialization before the tests start.
   */
  @Before
  public void initilize() {
    String directory = System.getProperty(USER_DIR) + "/xml/test";
    // export the current data for backup
    boolean isExported = myIsernModel.exportXml(
        System.getProperty(USER_DIR) + "/researchers.tmp.xml", 
        System.getProperty(USER_DIR) + "/organizations.tmp.xml",
        System.getProperty(USER_DIR) + "/collaborations.tmp.xml");
    assertTrue("Checking data export", isExported);
    
    // import the test data
    boolean isImported = myIsernModel.importResearchersXml(directory + "/researchers.test.xml");
    assertTrue("Checking researchers XML file import", isImported);
    isImported = myIsernModel.importOrganizationsXml(directory + "/organizations.test.xml");
    assertTrue("Checking organizations XML file import", isImported);
    isImported = myIsernModel.importCollaborationsXml(directory + "/collaborations.test.xml");
    assertTrue("Checking collaborations XML file import", isImported);
  }
  
  /**
   * Tests the normal operations of MyIsernModel.
   * 
   * @throws Exception If problems occur.
   */
  @Test
  public void testNormalOperations() throws Exception {
    // import the test XML files
    String directory = System.getProperty(USER_DIR) + "/xml/test";
    boolean isImported = true;
    try {
      isImported = myIsernModel.importResearchersXml(directory + "/testFile.in.txt");
      isImported = myIsernModel.importOrganizationsXml(directory + "/testFile.in.txt");
      isImported = myIsernModel.importCollaborationsXml(directory + "/testFile.in.txt");
    }
    catch (Exception e) {
      fail("This should throw exception.");
    }
    assertFalse("Checking invalid XML files import", isImported);
    
    
    // re-create the database tables
    if (!myIsernModel.createTables()) {
      fail("Fail because the database tables are not created.");
    }
    
    // Verifies methods for retrieving entire list of researchers, organizations, or collaborations.
    List<Researcher> researchers = myIsernModel.getResearchersList();
    assertEquals("Checking researchers size", 3, researchers.size());
    List<Organization> organizations = myIsernModel.getOrganizationsList();
    assertEquals("Checking organization size", 3, organizations.size());
    List<Collaboration> collaborations = myIsernModel.getCollaborationsList();
    assertEquals("Checking collaborations size", 3, collaborations.size());
   
    // verifies the addOrEdit methods for researcher
    String name = "";
    Researcher newResearcher = new Researcher();
    name = "New Researcher";
    newResearcher.setName(name);
    String email = "new@gmail.com";
    newResearcher.setEmail(email);
    myIsernModel.addOrEditResearcher(newResearcher);
    Researcher researcher = myIsernModel.getSingleResearcher(name);
    assertEquals("Checking researcher email", email, researcher.getEmail());
    
    email = "new@hotmail.com";
    researcher.setEmail(email);
    myIsernModel.addOrEditResearcher(researcher);
    researcher = myIsernModel.getSingleResearcher(name);
    assertEquals("Checking researcher email after editing", email, researcher.getEmail());
    
    // verifies the addOrEdit methods for organization
    name = "New Organization";
    Organization newOrganization = new Organization();
    newOrganization.setName(name);
    String country = "USA";
    newOrganization.setCountry(country);
    // provide necessary info to prevent null values
    AffiliatedResearchers affiliatedResearchers = new AffiliatedResearchers();
    newOrganization.setAffiliatedResearchers(affiliatedResearchers);
    ResearchKeywords researchKeywords = new ResearchKeywords();
    newOrganization.setResearchKeywords(researchKeywords);
    myIsernModel.addOrEditOrganization(newOrganization);
    Organization organization = myIsernModel.getSingleOrganization(name);
    assertEquals("Checking organization country", country, organization.getCountry());
    country = "China";
    organization.setCountry(country);
    myIsernModel.addOrEditOrganization(organization);
    organization = myIsernModel.getSingleOrganization(name);
    assertEquals("Checking organization country after editing", 
        country, organization.getCountry());
    
    // verifies the addOrEdit methods for collaboration
    name = "New Collaboration";
    Collaboration newCollaboration = new Collaboration();
    newCollaboration.setName(name);
    String description = "A new collaboration.";
    newCollaboration.setDescription(description);
    // provide necessary info to prevent null values
    CollaboratingOrganizations collabOrgs = new CollaboratingOrganizations();
    newCollaboration.setCollaboratingOrganizations(collabOrgs);
    CollaborationTypes collabTypes = new CollaborationTypes();
    newCollaboration.setCollaborationTypes(collabTypes);
    Years years = new Years();
    newCollaboration.setYears(years);
    OutcomeTypes outcome = new OutcomeTypes();
    newCollaboration.setOutcomeTypes(outcome);
    myIsernModel.addOrEditCollaboration(newCollaboration);   
    Collaboration collaboration = myIsernModel.getSingleCollaboration(name);
    assertEquals("Checking collaboration description",
        description, collaboration.getDescription());
    description = "An old collaboration.";
    collaboration.setDescription(description);
    myIsernModel.addOrEditCollaboration(collaboration);
    collaboration = myIsernModel.getSingleCollaboration(name);
    assertEquals("Checking collaboration description after editing", 
        description, collaboration.getDescription());
    
    // export the test XML files    
    boolean isExported = myIsernModel.exportXml(directory + "/researchers.export.xml",
                                                directory + "/organizations.export.xml",
                                                directory + "/collaborations.export.xml");
    assertTrue("Checking XML files import", isExported);
    
    // verifies the remove methods for researcher
    boolean isRemoved;
    assertEquals("Checking reseachers size before remove", 4, 
        myIsernModel.getResearchersList().size());
    isRemoved = myIsernModel.removeResearcher("New Researcher");
    assertTrue("Checking removal for researchers", isRemoved);
    assertEquals("Checking reseachers size after remove", 3, 
        myIsernModel.getResearchersList().size());
    isRemoved = myIsernModel.removeResearcher("No Such Researcher");
    assertFalse("Checking error removal for researchers", isRemoved);
    
    // verifies the remove methods for organization
    assertEquals("Checking organizations size before remove", 4, 
        myIsernModel.getOrganizationsList().size());
    isRemoved = myIsernModel.removeOrganization("New Organization");
    assertTrue("Checking removal for organizations", isRemoved);
    assertEquals("Checking organizations size after remove", 3, 
        myIsernModel.getOrganizationsList().size());
    isRemoved = myIsernModel.removeOrganization("No Such Organization");
    assertFalse("Checking error removal for organizations", isRemoved);
    
    // verifies the remove methods for collaboration
    assertEquals("Checking collaborations size before remove", 4, 
        myIsernModel.getCollaborationsList().size());
    isRemoved = myIsernModel.removeCollaboration("New Collaboration");
    assertTrue("Checking removal for collaborations", isRemoved);
    assertEquals("Checking collaborations size after remove", 3, 
        myIsernModel.getCollaborationsList().size());
    isRemoved = myIsernModel.removeCollaboration("No Such Collaboration");
    assertFalse("Checking error removal for collaborations", isRemoved);
  }
  
  /**
   * Tests the helper methods of MyIsernModel
   * 
   * @throws Exception If problems occur.
   */
  @Test
  public void testHelperMethods() throws Exception {
    // test the file copying method
    File in = new File(System.getProperty(USER_DIR) + "/no_such_file");
    File out = null;
    try {
      this.myIsernModel.copyFile(in, out);
    }
    catch (Exception e) {
      fail("This should throw exception.");
      assertNull("Testing invalid file copying", out);
    }
    in = new File(System.getProperty(USER_DIR) + "/xml/test/testFile.in.txt");
    out = new File(System.getProperty(USER_DIR) + "/xml/test/testFile.out.txt");
    this.myIsernModel.copyFile(in, out);
    assertNotNull("Testing valid file copying", out);
    
    // test the sorting of researchers
    this.myIsernModel.sortResearchers();
    String stringsOfNames = this.myIsernModel.getResearcherNamesList();
    assertEquals("Checking the sorting of researchers", 
        "Marvin Zelkowitz|Philip Johnson|Victor Basili", stringsOfNames);
    
    // test the sorting of organizations
    this.myIsernModel.sortOrganizations();
    stringsOfNames = this.myIsernModel.getOrganizationNamesList();
    assertEquals("Checking the sorting of organizations", 
        "Avaya Labs Research|University of Hawaii|University of Maryland", stringsOfNames);
    
    // test the sorting of collaborations
    this.myIsernModel.sortCollaborations();
    stringsOfNames = this.myIsernModel.getCollaborationNamesList();
    assertEquals("Checking the sorting of collaborations", 
        "UH-IESE-SIM-NARA|UH-US-VTT|UKL-UH", stringsOfNames);
  }
  
  /**
   * Recover the data after testing.
   */
  @After
  public void recover() {
    // import from the tmp files to restore the data
    boolean isImported = myIsernModel.
        importResearchersXml(System.getProperty(USER_DIR) + "/researchers.tmp.xml");
    assertTrue("Checking researchers XML file import", isImported);
    isImported = myIsernModel.
        importOrganizationsXml(System.getProperty(USER_DIR) + "/organizations.tmp.xml");
    assertTrue("Checking organizations XML file import", isImported);
    isImported = myIsernModel.
        importCollaborationsXml(System.getProperty(USER_DIR) + "/collaborations.tmp.xml");
    assertTrue("Checking collaborations XML file import", isImported);
    
    // delete the tmp files
    File file = new File(System.getProperty(USER_DIR) + "/researchers.tmp.xml");
    boolean isDeleted = file.delete();
    assertTrue("Checking researchers tmp file deletion", isDeleted);
    file = new File(System.getProperty(USER_DIR) + "/organizations.tmp.xml");
    file.delete();
    assertTrue("Checking organizations tmp file deletion", isDeleted);
    file = new File(System.getProperty(USER_DIR) + "/collaborations.tmp.xml");
    file.delete();
    assertTrue("Checking collaborations tmp file deletion", isDeleted);
  }
}